package servlets;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import modelo.bean.LatLng;
import modelo.bean.Trajeto;

import testeMapas.FachadaDirectionsResult;

public class GetTrajetoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String num = request.getParameter("num");
		if (num == null) {
			System.out.println("o num veio null...");
			num = "0";
		}
		System.out.println("Via get: Get carona servlet: " + num);
		FachadaDirectionsResult fachada = FachadaDirectionsResult.getInstance();
		String result = null;
		Trajeto d = fachada.procurarPorCodigo(Long.parseLong(num));
		
		System.out.println(d.getStart() + " at� " + d.getEnd());
				
		int i = 0;
		for (LatLng lg : d.getPassos()) {
			if (result == null) {
				result = lg.getLat() + "," + lg.getLng() + ",";
			} else if (i == d.getPontos().size() - 1) {
				result += lg.getLat() + "," + lg.getLng();
			} else {
				result += lg.getLat() + "," + lg.getLng() + ",";
			}
			i++;
		}
		response.setContentType("text/plain");
		response.getWriter().write(result);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String num = request.getParameter("num");
		if (num == null) {
			System.out.println("o num veio null...");
			num = "0";
		}
		System.out.println("Get carona servlet: " + num);
		FachadaDirectionsResult fachada = FachadaDirectionsResult.getInstance();
		String result = null;
		Trajeto d = fachada.procurarPorCodigo(Long.parseLong(num));
		int i = 0;
		
		System.out.println(d.getStart() + " ate " + d.getEnd());
		
		for (LatLng lg : d.getPassos()) {
			if (result == null) {
				result = lg.getLat() + "," + lg.getLng() + ",";
			} else if (i == d.getPassos().size() - 1) {
				result += lg.getLat() + "," + lg.getLng();
			} else {
				result += lg.getLat() + "," + lg.getLng() + ",";
			}
			i++;
		}
		response.setContentType("text/plain");
		response.getWriter().write(result);
	}
}